import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QListWidgetItem
from PyQt5.QtGui import QColor, QBrush

class PlaylistWindow(QWidget):
    def __init__(self):
        super().__init__()
        # 初始化 UI
        self.initUI()

    def initUI(self):
        # 创建一个垂直布局
        layout = QVBoxLayout()

        # 创建一个列表部件用于显示歌单
        self.list = QListWidget()
        # 向列表部件中添加歌曲
        for i in range(30):
            song = f"Song_{i + 1}.mp3"
            item = QListWidgetItem(song)
            self.list.addItem(item)
        self.list.itemClicked.connect(self.signal_choose_item) # 点击列表
        self.list.currentItemChanged.connect(self.signal_current_item_changed) # 列表项改变

        # 将列表部件添加到布局中
        layout.addWidget(self.list)

        # 设置窗口布局
        self.setLayout(layout)
        # 设置窗口标题
        self.setWindowTitle('list列表')
        # 设置窗口大小
        self.setGeometry(300, 300, 300, 400)

    def signal_choose_item(self, item):
        print(f"choose item:{self.list.row(item)}, {item.text()}")

    def signal_current_item_changed(self, current, previous):
        print(f"change item:{self.list.row(current)}, {current.text()}")
        print(f"change item:{self.list.row(previous)}, {previous.text()}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = PlaylistWindow()
    window.show()
    sys.exit(app.exec_())